const app = getApp()
Component({
	/**
	 * 组件的初始数据
	 */
	data: {
		color: '#7A7E83',
		selectedColor: '#ff9900',
		tabBarList: [
			{
				iconPath:
					'https://llz-img-resources.oss-cn-hangzhou.aliyuncs.com/llz-mina-img/tabbar/tabbar_home_icon.png',
				selectedIconPath:
					'https://llz-img-resources.oss-cn-hangzhou.aliyuncs.com/llz-mina-img/tabbar/tabbar_home_active_icon.png',
				pagePath: '../index/index',
				text: '首页'
			},
			{
				iconPath:
					'https://llz-img-resources.oss-cn-hangzhou.aliyuncs.com/llz-mina-img/tabbar/tabbar_order_icon.png',
				selectedIconPath:
					'https://llz-img-resources.oss-cn-hangzhou.aliyuncs.com/llz-mina-img/tabbar/tabbar_order_active_icon.png',
				pagePath: '../order/order',
				text: '订单'
			},
			{
				iconPath:
					'https://llz-img-resources.oss-cn-hangzhou.aliyuncs.com/llz-mina-img/tabbar/tabbar_mall_icon.png',
				selectedIconPath:
					'https://llz-img-resources.oss-cn-hangzhou.aliyuncs.com/llz-mina-img/tabbar/tabbar_mall_active_icon.png',
				pagePath: 'plugin://sy-plugin/index',
				text: '商城'
			},
			{
				iconPath:
					'https://llz-img-resources.oss-cn-hangzhou.aliyuncs.com/llz-mina-img/tabbar/tabbar_my_icon.png',
				selectedIconPath:
					'https://llz-img-resources.oss-cn-hangzhou.aliyuncs.com/llz-mina-img/tabbar/tabbar_my_active_icon.png',
				pagePath: '../logs/logs',
				text: '我的'
			}
		],
		selected: 0
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		switchTab(e) {
			const data = e.currentTarget.dataset
			const url = data.path

			if (data.index === 2 && !app.globalData.userId) {
				// 不是会员的情况下，需要先注册会员
				wx.showModal({
					content: '您需要注册会员',
					success: res => {
						res.confirm &&
							wx.navigateTo({
								url: '../register/register'
							})
					}
				})
				return
			}

			wx[data.index === 2 ? 'navigateTo' : 'switchTab']({
				url: `${url}?userId=${app.globalData.userId}`,
				success: res => {
					console.log('跳转成功', res)
				},
				fail: err => {
					console.log('跳转失败', err)
				}
			})
			console.log(data.index)
			this.setData({
				selected: data.index
			})
		}
	}
})
